//
//  CommentViewController.m
//  Streaming
//
//  Created by 官琛 on 15-6-27.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "CommentViewController.h"

@interface CommentViewController ()

@end

@implementation CommentViewController

- (void)dealloc
{
    [self.tableView release];
    [self.array release];
    [self.commentArray release];
    [self.image release];
    [self.label release];
    [self.HUD release];
    [self.movieDetail release];
    [self.allView release];
    [self.allViewModel release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[DateHandel shareHandle]createCommentTable];
    self.navigationItem.title = @"影评";
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
//    self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    if (WIDTH == 320 && HEIGHT == 568) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }
    if (WIDTH == 414 && HEIGHT == 736) {
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
    }
    if (WIDTH == 375 && HEIGHT == 667){
        self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
    }

    
    self.allView.allViewDelegate = self;
    self.tableView.tableHeaderView = self.allView;
    
    [self afnGetRequest];
    [self allAfnGetRequest];
    [self headerRefesh];
    [_tableView autorelease];
    
    
}

- (void)presentAllView
{
    commentDetailsController *view = [[commentDetailsController alloc]init];
    view.comID = self.allViewModel.allID;
    [self.navigationController pushViewController:view animated:YES];
    [view autorelease];
    
}
//下拉 上啦刷新
-(void)headerRefesh
{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
        
        if (WIDTH == 320 && HEIGHT == 568) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 320)];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            self.allView = [[AllView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 260)];
        }
        
        [self allAfnGetRequest];
        self.allView.allViewDelegate = self;
        self.tableView.tableHeaderView = self.allView;
        [self.allView release];

        
    }];

   
}




//titleView  Button
-(void)buttonAction:(UIButton *)button
{
//    NSLog(@"播放  movie = %ld", self.movieID);
    self.movieDetail = [[MovieDetailViewController alloc] init];
    self.movieDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:self.movieDetail animated:YES];
    self.movieDetail.movieID = self.movieID;
//    NSLog(@"===%ld", self.movieDetail.movieID);
    [self.movieDetail autorelease];

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    commentDetailsController *view = [[commentDetailsController alloc]init];
    view.comID = [[self.commentArray objectAtIndex:indexPath.row]integerValue];
    [self.navigationController pushViewController:view animated:YES];
    [view autorelease];

    

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInfer = @"commentCell";
    commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfer];
    if (cell == nil) {
        cell = [[commentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfer];
    }
    coment *com = [self.array objectAtIndex:indexPath.row];
    cell.com = com;
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;

}

-(void)afnGetRequest
{
    
    self.array = [NSMutableArray array];
    self.array = [[DateHandel shareHandle]selectAllComment];
    //详情http://api.m.mtime.cn/Review/Detail.api?reviewId=7902865
    //http://api.m.mtime.cn/MobileMovie/Review.api?needTop=false
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/MobileMovie/Review.api";
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//            NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        [[DateHandel shareHandle]dropCommentTable];
        [[DateHandel shareHandle]createCommentTable];
        [self.array removeAllObjects];
        
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:responseObject];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic = [array objectAtIndex:0];
        self.movieID = [[dic objectForKey:@"id"]integerValue];
        self.label.text = [dic objectForKey:@"title"];
        NSString *str = [dic objectForKey:@"image"];
        NSURL *url = [NSURL URLWithString:str];
        [self.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"111111.png"]];
        
        
        [array removeObjectAtIndex:0];
        
        self.commentArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            coment *come = [[coment alloc]init];
            come.title = [dic objectForKey:@"title"];
            come.userImage = [dic objectForKey:@"userImage"];
            come.summary = [dic objectForKey:@"summary"];
            come.idd = [[dic objectForKey:@"id"]integerValue];
            come.nickname = [dic objectForKey:@"nickname"];
            NSMutableDictionary *dic1 = [dic objectForKey:@"relatedObj"];
            come.movieImage = [dic1 objectForKey:@"image"];
            come.movieTitle = [dic1 objectForKey:@"title"];
            [self.array addObject:come];
            [self.commentArray addObject:[NSString stringWithFormat:@"%ld",come.idd]];
            [[DateHandel shareHandle]insertComment:come];
            [come autorelease];
        }
//        NSLog(@"%ld",self.array.count);
        
        [self.tableView reloadData];
        [self.HUD hide:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        NSLog(@"失败==== %@",error);
        
//        NSURL *url = [NSURL URLWithString:url_string];
//        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
//        NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//        NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",str);
//    
//        [request setValue:@"24235" forHTTPHeaderField:@"wgrwg"];
//        
//        [request setHTTPBody:received];
        
        
        
        
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];
                [self.HUD hide:YES];
        
        
        
    }];
    
}

- (void)allAfnGetRequest
{
    self.allViewModel = [[AllViewModel alloc] init];
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    NSLog(@"url = %@", url_string);
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //                NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSMutableDictionary *mineDic = [NSMutableDictionary dictionary];
        mineDic = [responseObject objectForKey:@"review"];
        //            NSLog(@"mineDic = %@", mineDic);
        self.allViewModel.allTitle = [mineDic objectForKey:@"title"];
        NSLog(@"title = %@", self.allViewModel.allTitle);
        self.allViewModel.allImageUrl = [mineDic objectForKey:@"imageUrl"];
        self.allViewModel.allMp4Url = [mineDic objectForKey:@"mp4Url"];
        self.allViewModel.allType = [[mineDic objectForKey:@"type"]integerValue];
        self.allViewModel.allID = [[mineDic objectForKey:@"reviewID"]integerValue];
        NSLog(@"self = %@", self.allViewModel);
        self.allView.AllModel = self.allViewModel;
        
        [self.tableView reloadData];
        
        
        [self.HUD hide:YES];
        [self.tableView.footer endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];
    }];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
